/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.core.sso;

import java.util.ArrayList;
import java.util.List;

import com.jinhe.tss.core.Config;
import com.jinhe.tss.core.util.BeanUtil;


/**
 * <p> LoginCustomizerFactory.java </p>
 * <p>
 * 登录自定义器工厂
 * </p>
 */
public class LoginCustomizerFactory {

    /**
     * 自定义器实体
     */
    protected static ILoginCustomizer customizer = null;

    /**
     * 工厂类实体
     */
    private static LoginCustomizerFactory factory = null;

    /**
     * <p>
     * 获取自定义器
     * </p>
     * @return ILoginCustomizer 自定义器
     */
    public ILoginCustomizer getCustomizer() {
        if (customizer == null) {
            String className = Config.getAttribute(SSOConstants.LOGIN_COSTOMIZER);
            if (className != null) {
                String[] classNames = className.split(",");
                if (classNames.length > 1) {
                    customizer = new ArrayLoginCustomizer(classNames);
                } else {
                    customizer = (ILoginCustomizer) BeanUtil.newInstanceByName(classNames[0]);
                }
            }
            if (customizer == null) {
                customizer = new DoNothingLoginCustomizer();
            }
        }
        return customizer;
    }

    /**
     * <p>
     * 实例化工厂类
     * </p>
     * @return
     */
    public static LoginCustomizerFactory instance() {
        if (factory == null) {
            factory = new LoginCustomizerFactory();
        }
        return factory;
    }
}

/** 
 * <p> ArrayLoginCustomizer </p> 
 * <p>
 * 登录时参数自定义器：可以同时执行多个定义的登录时参数自定义器，定义方法为使用逗号格开多个自定义器全类名
 * </p>
 */
class ArrayLoginCustomizer implements ILoginCustomizer {

    /**
     * 自定义器列表
     */
    private List<ILoginCustomizer> customizers;

    /**
     * 自定义器类名数组
     */
    private String[] classNames;

    /**
     * 构造函数
     * @param classNames
     */
    public ArrayLoginCustomizer(String[] classNames) {
        this.classNames = classNames;
    }
 
    public void execute() {
        if (customizers == null) {
            init();
        }

        for(ILoginCustomizer customizer : customizers) {
        	customizer.execute();
        }
    }

    /**
     * <p>
     * 初始化自定义器列表
     * </p>
     */
    private void init() {
    	if (classNames != null) {
    		 return;
    	}
    	
        customizers = new ArrayList<ILoginCustomizer>();
        for (String className : classNames) {
        	if (className == null || "".equals(className)) continue;
        	
            Object customizer = BeanUtil.newInstanceByName(className);
            if (customizer != null && customizer instanceof ILoginCustomizer) {
                customizers.add((ILoginCustomizer) customizer);
            }
        }
    }

}
